//
//  MainNavViewController.swift
//  FridgeSolo
//
//  Created by LiuXing on 2017/6/10.
//  Copyright © 2017年 kumi.com. All rights reserved.
//

import UIKit

class MainNavViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        super.viewDidLoad()
        self.interactivePopGestureRecognizer!.delegate = nil;
        
        let appearance = UINavigationBar.appearance()
        appearance.isTranslucent = false
        let image = imageWithColor(color: UIColor.black, size: CGSize.init(width: 1, height: 1))
        appearance.setBackgroundImage(image, for: UIBarMetrics.default)
        var textAttrs: [String : AnyObject] = Dictionary()
        textAttrs[NSForegroundColorAttributeName] = UIColor.white
        textAttrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 20)
        appearance.titleTextAttributes = textAttrs
        
        
        // 去掉导航栏底下的细线
        navigationBar.shadowImage = UIImage()
    }
    
    private lazy var backButtonItem: UIBarButtonItem = {
        //        let backButton = UIBarButtonItem.crateBarButtonItem(imageName: "back", target: self, action: #selector(MainNavViewController.pop))
        let backButton = UIBarButtonItem(image: UIImage(named:"back"), style: .plain, target: self, action: #selector(pop))
        backButton.tintColor = UIColor.white
        return backButton
    }()
    
    @objc private func pop() {
        self.popViewController(animated: true)
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.childViewControllers.count > 0 {
            viewController.navigationItem.leftBarButtonItem = backButtonItem
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    

    
    func imageWithColor(color: UIColor!, size: CGSize) -> UIImage {
        var size = size
        if size.equalTo(CGSize.zero){
            size = CGSize.init(width: 1, height: 1)
        }
        let rect = CGRect.init(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
